/*
 * NFC_reader.c
 *
 * Created: 20.02.2013 14:31:43
 *  Author: saenko
 */ 


#include <avr/io.h>
#include <stdio.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/delay.h>

#define F_CPU 8000000UL

#define cbi(reg,bit)	reg &= ~(_BV(bit))
#define sbi(reg,bit)	reg |= (_BV(bit))


#include "uart.h"
#include "SPI.h"
#include "HEXtoASCII.h"
#include "trf7960.h"



#define IDLE 0
#define READ_ONE 1
#define WRITE_ONE 2
#define OUT_REGS 3
#define WR_DEF_REG 4
#define WR_REG 5
#define READ_TAG 6
#define SET_CHIP 7
#define LOOP 8
#define LOOP_1 9

unsigned char state = IDLE;

unsigned char buf_old[16];

unsigned char change_buf;
int main(void)
{	
	sbi(DDRD,5);

	uart_init();
	spi_init();
	trf7960_init();
	sei();
	
	printstr_p(PSTR("ATMega88 NEW\n"));	
	
    while(1)
    {
        if(uart_flags.rxstring_end)
		{
			uart_flags.rxstring_end = 0;
			switch(rxstring[0])
			{
			case 'R':
					state = READ_ONE;
			break;

			case 'W':
					state = WRITE_ONE;
			break;
			
			case 'O':
					state = OUT_REGS;
			break;
			
			case 'D':
					state = WR_DEF_REG;
			break;
			
			case 'd':
					state = WR_REG;
			break;			
			
			case 'E':
					sbi(PORTD,5);
			break;
			
			case 'e':
					cbi(PORTD,5);
			break;
			
			case 'T':
					state = READ_TAG;
			break;
			
			case 'C':
					state = SET_CHIP;
			break;
			
			case 'L':
					state = LOOP;
			break;
			
			case 'l':
					state = IDLE;
			break;
			
			case 'K':
					state = LOOP_1;
			break;
			}
	
		}
		
		switch(state)
		{
		case READ_ONE:
			state = IDLE;
			putchr(spi_read_one_reg(0x00));
		break;
			
		case WRITE_ONE:
			state = IDLE;
			spi_write_one_reg(0x00,0x0F);
		break;
		
		case OUT_REGS:
			state = IDLE;
			out_regs();
		break;
		
		case WR_DEF_REG: //D
			state = IDLE;
			buf[0]=0x20;
			buf[1]=0x21;
			buf[2]=0x03;
			spi_write_from_buf(3);
		break;
				
		case WR_REG:  //d
			state = IDLE;
			buf[0]=0x20;
			buf[1]=0x01;
			buf[2]=0x02;
			spi_write_from_buf(3);
		break;
		
		case READ_TAG:
			state = IDLE;
	//		0x8f - 0x91 -0x3d - 0x00 - 0x30 - 0x26 - 0x01 - 0x00
			buf[0]=0x8F;
			buf[1]=0x90;
			buf[2]=0x3D;
			buf[3]=0x00;
			buf[4]=0x0F;
			buf[5]=0x52;
			spi_write_from_buf(6);
		break;		
		
		case SET_CHIP:
			state = IDLE;
			buf[0]=0x20;
			buf[1]=0x21;
			buf[2]=0x88;//02
			buf[3]=0x00;
			buf[4]=0x00;
			buf[5]=0xC8;
			buf[6]=0xBB;
			buf[7]=0x20;
			buf[8]=0x0E;
			buf[9]=0x07;
			buf[10]=0x21;
			buf[11]=0x43;
			buf[12]=0x87;
			spi_write_from_buf(13);
		break;
		
		case LOOP:
			sbi(PORTD,5);
			
			_delay_ms(10);
			
			buf[0]=0x20;
			buf[1]=0x21;
			buf[2]=0x02;
			buf[3]=0x00;
			buf[4]=0x00;
			buf[5]=0xC1;
			buf[6]=0xBB;
			buf[7]=0x00;
			buf[8]=0x30;
			buf[9]=0x1F;
			buf[10]=0x21;
			buf[11]=0x43;
			buf[12]=0x87;
			spi_write_from_buf(13);
			
			_delay_ms(10);
			
			buf[0]=0x8F;
			buf[1]=0x91;
			buf[2]=0x3D;
			buf[3]=0x00;
			buf[4]=0x30;
			buf[5]=0x26;
			buf[6]=0x01;
			buf[7]=0x00;
			spi_write_from_buf(8);
			
			_delay_ms(100);
			
			if(RX_to_process)
			{
				RX_to_process = 0;
				
					for(unsigned char i = 0; i<FIFO_num; i++)
					{
						hex_to_ascii(buf[i]);
						putchr(ascii_hi);
						putchr(ascii_low);
						putchr(' ');

					}
					putchr(13);

					_delay_ms(5000);
			}
			
			cbi(PORTD,5);
		
		break;
		
		
		case LOOP_1:
						sbi(PORTD,5);
						
						_delay_ms(10);
						
						buf[0]=0x20;
						buf[1]=0x21;
						buf[2]=0x08;
						buf[3]=0x00;
						buf[4]=0x00;
						buf[5]=0xC1;
						buf[6]=0xBB;
						buf[7]=0x00;
						buf[8]=0x30;
						buf[9]=0x1F;
						buf[10]=0x21;
						buf[11]=0x43;
						buf[12]=0x87;
						spi_write_from_buf(13);
						
						_delay_ms(10);
						
						buf[0]=0x8F;
						buf[1]=0x90;
						buf[2]=0x3D;
						buf[3]=0x00;
						buf[4]=0x0F;
						buf[5]=0x26;
						
						spi_write_from_buf(8);
						
						_delay_ms(100);
						
						if(RX_to_process)
						{
							RX_to_process = 0;
							
							for(unsigned char i = 0; i<FIFO_num; i++)
							{
								hex_to_ascii(buf[i]);
								putchr(ascii_hi);
								putchr(ascii_low);
								putchr(' ');

							}
							putchr(13);

							_delay_ms(5000);
						}
						
						cbi(PORTD,5);
						
		break;
		}		
	
		

		
	}
}

//----------------------------

